package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.ConditionConstant;
import com.xbongbong.paas.constant.ConditionForChartConstant;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.clazz.ConditionClass;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.parent.util.ConditionUtil;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.ConditionOperateFlagEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2020/6/24 9:31
 * @since v1.0
 */
public class ConditionListHelp {
    private static final Logger LOG = LoggerFactory.getLogger(ConditionListHelp.class);
    public static List<ConditionClass> getConditionList(FieldTypeEnum fieldTypeEnum , Integer forChart) throws XbbException {
        List<ConditionClass> conditionList = new ArrayList<>();
        try {
            switch (fieldTypeEnum) {
                case DATETIME:
                    String[] datetime = ConditionConstant.DATETIME;
                    if(Objects.equals(ConditionOperateFlagEnum.FROM_CHART.getCode(),forChart)){
                        datetime = ConditionForChartConstant.DATETIME;
                    }
                    conditionList = ConditionEnum.getConditionList(datetime);
                    break;
                case TEXT:
                    String[] text = ConditionConstant.TEXT;
                    if(Objects.equals(Objects.equals(forChart,ConditionOperateFlagEnum.FROM_CHART.getCode()),forChart) || Objects.equals(forChart,ConditionOperateFlagEnum.FROM_PUBLIC.getCode())){
                        text = ConditionForChartConstant.TEXT;
                    }
                    conditionList = ConditionEnum.getConditionList(text);
                    break;
                case RADIO_GROUP:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.RADIO_GROUP);
                    break;
                case COMBO:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.COMBO);
                    break;
                case COMBO_NUM:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.COMBO_NUM);
                    break;
                case USER:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.USER);
                    break;
                case DEPT:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.DEPT);
                    break;
                case USER_GROUP:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.USER_GROUP);
                    break;
                case DEPT_GROUP:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.DEPT_GROUP);
                    break;
                case ADDRESS:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.ADDRESS);
                    break;
                case LOCATION:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.LOCATION);
                    break;
                case TEXTAREA:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.TEXTAREA);
                    break;
                case NUM:
                case STAGE_RATIO:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.NUM);
                    break;
                case CHECKBOX_GROUP:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.CHECKBOX_GROUP);
                    break;
                case COMBO_CHECK:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.COMBO_CHECK);
                    break;
                case UPLOAD:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.UPLOAD);
                    break;
                case IMAGE:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.IMAGE);
                    break;
                case CREATORID:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.CREATORID);
                    break;
                case ADDTIME:
                    String[] addtime = ConditionConstant.ADDTIME;
                    if(Objects.equals(ConditionOperateFlagEnum.FROM_CHART.getCode(),forChart)){
                        addtime = ConditionForChartConstant.ADDTIME;
                    }
                    conditionList = ConditionEnum.getConditionList(addtime);
                    break;
                case UPDATETIME:
                    String[] updatetime = ConditionConstant.UPDATETIME;
                    if(Objects.equals(ConditionOperateFlagEnum.FROM_CHART.getCode(),forChart)){
                        updatetime = ConditionForChartConstant.UPDATETIME;
                    }
                    conditionList = ConditionEnum.getConditionList(updatetime);
                    break;
                case DEPARTMENTID:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.DEPARTMENTID);
                    break;
                case OWNERID:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.OWNERID);
                    break;
                case COUSERID:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.COUSERID);
                    break;
                case SERIALNO:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SERIALNO);
                    break;
                case LINK_BUSINESS_SINGLE:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.LINK_BUSINESS_SINGLE);
                    break;
                case LINK_BUSINESS_MULTI:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.LINK_BUSINESS_MULTI);
                    break;
                case CONTACT_NUMBER:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.CONTACT_NUMBER);
                    break;
                case SELECT_PRODUCT:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SELECT_PRODUCT);
                    break;
                case SELECT_PURCHASE:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SELECT_PURCHASE);
                    break;
                case SELECT_CONTRACT:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SELECT_CONTRACT);
                    break;
                case SPECIFICATION:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SPECIFICATION);
                    break;
                case RICH_TEXT_EDITOR:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.RICH_TEXT_EDITOR);
                    break;
                case BIRTHDAY:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.BIRTHDAY);
                    break;
                case STAR:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.STAR);
                    break;
                case PRODUCT_STOCK:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.PRODUCT_STOCK);
                    break;
                case ENDTIME:
                    String[] endtime = ConditionConstant.ENDTIME;
                    if(Objects.equals(ConditionOperateFlagEnum.FROM_CHART.getCode(),forChart)){
                        endtime = ConditionForChartConstant.ENDTIME;
                    }
                    conditionList = ConditionEnum.getConditionList(endtime);
                    break;
                case CLOSETIME:
                    String[] closetime = ConditionConstant.CLOSETIME;
                    if(Objects.equals(ConditionOperateFlagEnum.FROM_CHART.getCode(),forChart)){
                        closetime = ConditionForChartConstant.CLOSETIME;
                    }
                    conditionList = ConditionEnum.getConditionList(closetime);
                    break;
                case WORK_ORDER_TIME_OUT:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.WORK_ORDER_TIME_OUT);
                    break;
                case WORK_ORDER_STATUS:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.STATUS);
                    break;
                case WORK_ORDER_STAGE:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.STAGE_ID);
                    break;
                case TEXT_CHECK:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.TEXT_CHECK);
                    break;
                case WORK_ORDER_NODE_STATUS:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.NUM_3);
                    break;
                case CREATOR_DEPT:
                    // 流程用 创建人部门
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.CREATOR_DEPT);
                    break;
                case PUBLIC_GROUP_PROVINCE:
                    // 公海分组-省特殊选择
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.PUBLIC_GROUP_PROVINCE);
                    break;
                case CATEGORY_ID:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.CATEGORY_ID);
                    break;
                case LINK_DATA:
                    // 关联数据
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.LINK_DATA);
                    break;
                case LINK_DATA_MULTI:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.LINK_DATA_MULTI);
                    break;
                case SWITCH:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SWITCH);
                    break;
                case BATCH:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.BATCH);
                    break;
//                case LAST_CONNECT_TIME:
//                    conditionList = ConditionEnum.getConditionList(ConditionConstant.LAST_CONNECT_TIME);
//                    break;
                case SEQ:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SEQ);
                    break;
                case LABEL:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.LABEL);
                    break;
                case SCORE_RANGE:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.SCORE_RANGE);
                    break;
                case BUSINESS_TYPE:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.BUSINESS_TYPE);
                    break;
                case AREA:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.AREA);
                    break;
                case CHECK_DATE:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.CHECK_DATE);
                    break;
                case PURCHASEABLE_GOODS:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.PURCHASEABLE_GOODS);
                    break;
                case STAGE_THRUSTER:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.STAGE_THRUSTER);
                    break;
                case PUBLIC_GROUP:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.PUBLIC_GROUP);
                    break;
                case NODE_ID:
                    conditionList = ConditionEnum.getConditionList(ConditionConstant.NODE_ID);
                    break;
                default:
                    LOG.error("===="+fieldTypeEnum.getType());
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
        } catch (Exception e) {
            LOG.error("condition数据库根据类型获取筛选条件列表出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return conditionList;
    }

    /**
     * 校验是否为同一表单
     * @param targetField
     * @param fieldAttrEntity
     * @param thisFormId
     * @param targetFormId
     * @throws XbbException
     */
    // TODO 关联数据多选怎么处理 by HU
    public static void validateTheForm(FieldAttrEntity targetField, FieldAttrEntity fieldAttrEntity, Long thisFormId, Long targetFormId, Integer linkBusinessType, Integer businessType) throws XbbException {
        ConditionUtil.validateTheForm(targetField, fieldAttrEntity, thisFormId, targetFormId, linkBusinessType, businessType);
    }


    /**
     * 钩子方法：对数据进行判空
     * @param value
     * @return
     */
    public static Boolean isEmpty(Object value, Integer fieldType) {
        boolean isAddress = (Objects.equals(FieldTypeEnum.ADDRESS.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.LOCATION.getType(), fieldType)) && value instanceof Map;
        if (isAddress) {
            JSONObject address = JSONObject.parseObject(JSON.toJSONString(value));
            if (Objects.isNull(address) || StringUtil.isEmpty(address.getString(BusinessConstant.PROVINCE))) {
                return true;
            }
            return false;
        } else {
            boolean isEmpty = value instanceof List && CollectionsUtil.isEmpty(((List)value));
            return Objects.isNull(value) || StringUtil.isEmpty(value.toString()) || isEmpty;
        }
    }

    /**
     * 钩子方法：判断当前字段是关联数据，目标字段不是关联数据、数据id、关联业务单选，便于解析主键字段
     * @param fieldType 当前字段的字段类型
     * @param targetFieldType 目标字段的字段类型
     * @return
     */
    public static Boolean validateIsLinkData(Integer fieldType, Integer targetFieldType) {
        return Objects.equals(fieldType, FieldTypeEnum.LINK_DATA.getType())
                && !Objects.equals(FieldTypeEnum.LINK_DATA.getType(), targetFieldType)
                && !Objects.equals(FieldTypeEnum.DATAID.getType(), targetFieldType)
                && !Objects.equals(FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), targetFieldType);
    }
}
